﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Simulation
{
	class C_RPBS
	{
		C_COMM	CM = new C_COMM();
		C_NODE	ND = new C_NODE();
		F_SETT	ST = new F_SETT();

		object PN;

		float X1;
		float X2;
		float Y1;
		float Y2;

		public C_RPBS()
		{
			this.X1 = -1;
			this.X2 = -1;
			this.Y1 = -1;
			this.Y2 = -1;
		}

		public void SetLocation( float x1, float x2, float y1, float y2 )
		{
			this.X1 = x1;
			this.X2 = x2;
			this.Y1 = y1;
			this.Y2 = y2;
		}

		public void SetPanel( object pn )
		{
			this.PN = pn;
		}
		
		public void MeasureCost( C_NODE[] ND )
		{
			for ( int i = 1; i < ST.TTND; i++ )
				ND[i].COST = ( float )Math.Sqrt( ( ( ND[0].POSX - ND[i].POSX ) * ( ND[0].POSX - ND[i].POSX ) ) + ( ( ND[0].POSY - ND[i].POSY ) * ( ND[0].POSY - ND[i].POSY ) ) );
		}

		public float MeasureDistance()
		{
			return ( float )Math.Sqrt( ( ( X1 - X2 ) * ( X1 - X2 ) ) + ( ( Y1 - Y2 ) * ( Y1 - Y2 ) ) );
		}

		public void DrawRoutingPath( C_NODE[] ND )
		{
			for ( int p = 0; p < ST.TTND; p++ )
			{
				int mxct = 0;

				for ( int s = 1; s < ST.TTND; s++ )
				{

					SetLocation( ND[p].POSX, ND[s].POSX, ND[p].POSY, ND[s].POSY );
					float dst = MeasureDistance();

					if ( dst <= ST.RANG )
					{
						if ( ( p != s ) && ( ND[s].PRID == -1 ) )
						{
							if ( ND[p].COST < ND[s].COST )
							{
								ND[s].PRID = ND[p].NDID;
								DrawChildNode( ND[p].POSX, ND[s].POSX, ND[p].POSY, ND[s].POSY );

								if ( mxct >= ST.OMXCH )
									break;

								mxct++;
							}
						}
					}
				}
			}
		}

		public void DrawChildNode( float x1, float x2, float y1, float y2 )
		{
			Pen P		= new Pen( Color.DimGray, 1 );
			P.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
			Graphics G	= ( ( Panel ) PN ).CreateGraphics();

			G.DrawLine( P, x1, y1, x2, y2 );
		}

		public int MakeHopCount( int tgnd, C_NODE[] SN )
		{
			int mvnd = -1;
			int hpct = 0;

			while ( true )
			{
				mvnd = SN[tgnd].PRID;
				hpct++;
				tgnd = SN[mvnd].NDID;

				if ( SN[tgnd].PRID == -1 )
					break;
			}

			return hpct;
		}
	}
}
